<?php
App::uses('AppController', 'Controller');
App::import('Model','User');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {

	public function beforeFilter() {
		parent::beforeFilter();
	 	$this->Auth->authenticate = array(
	        'Form' => array (
	            'scope' => array('User.email_verified' => User::email_verified ),
							'fields' => array('username' => 'email')
	        )
	    );
			#$this->Auth->allow();
			#$this->Auth->deny(array('view'));
			$this->Auth->allow(array('login', 'verify'));
	}


	public function login() {
		  if ($this->request->is('post')) {
		      if ($this->Auth->login()) {
		          $this->redirect($this->Auth->redirect());
		      } else {
		          $this->Session->setFlash(__('Invalid username or password, try again'));
		      }
		  }
	}

	public function logout() {
		  $this->redirect($this->Auth->logout());
	}

	public function verify($token){
		$user = $this->User->findByEmailToken($token);
		if($user){
			$this->Session->setFlash(__('Verify successfully. Please login'));
			$user['User']['email_verified'] =  User::email_verified;
			$user['User']['email_token'] = null;
			$this->User->save($user);
		}else{
			$this->Session->setFlash(__('There is some wrong.<br/> Please contact to adminitrator'));
		}
		$this->redirect(array('action' => 'login'));
	}
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->User->create();
			$this->request->data['User']['created_at'] = date("Y-m-d H:i:s");
			$this->request->data['User']['email_token'] = AuthComponent::password(date("Y-m-d H:i:s"));
			$this->request->data['User']['status'] = User::active;
			if ($this->User->save($this->request->data)) {
				#send email verify
				App::uses('CakeEmail', 'Network/Email');
				$email = new CakeEmail('smtp');
				$email->template('verify_email');
				$email->emailFormat('text');
				$email->viewVars(array(	'email' => $this->request->data['User']['first_name']." ".$this->request->data['User']['last_name'],
																'token' => $this->request->data['User']['email_token']));
				$email->to($this->request->data['User']['email']);
				$email->subject('Account verification');
				$email->send();

				$this->Session->setFlash(__('The user has been saved. The verification email will be sent in the short time'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
		if(isset($this->request->query['email'])){
			$this->set('email', $this->request->query['email']);
		}
		if(isset($this->request->query['aid'])){
			$this->set('agent_id', $this->request->query['aid']);
		}
		$roles = $this->User->Role->find('list');
		$this->set(compact('roles'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->User->read(null, $id);
		}
		$roles = $this->User->Role->find('list');
		$this->set(compact('roles'));
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->User->delete()) {
			$this->Session->setFlash(__('User deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('User was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
